package com.qfedu.dao.impl;

import com.qfedu.dao.ProductDao;
import com.qfedu.model.Product;
import com.qfedu.utils.DruidUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;


public class ProductDaoImpl implements ProductDao {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtil.getDataSource());

    @Override
    public List<Product> findAll() {

        String sql = "select * from product";
        List<Product> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Product.class));
        return list;
    }

    @Override
    public Product findProByPid(int id) {
        System.out.println(111111);
        List<Product> list = jdbcTemplate.query("select * from product where pid = ?", new BeanPropertyRowMapper<>(Product.class), id);
        if (list!=null&&list.size()==1){
            return list.get(0);
        }
        System.out.println(1);
        return null;
    }

    @Override
    public int addProduct(Product product) {
        String sql = "insert into product values (null,?,?,?,?)";
        int update = jdbcTemplate.update(sql, product.getPname(), product.getPrice(), product.getFlag(),product.getCid());
//        System.out.println(update);
        return update;
    }

    @Override
    public int updatePro(Product product) {
        String sql = "update product set pname=?,price=?,flag=? where pid =?";
        int update = jdbcTemplate.update(sql, product.getPname(), product.getPrice(), product.getFlag(), product.getPid());
        return update;
    }

    @Override
    public int deleteProduct(int id) {
        String sql="delete from product where pid =?";
        int delete=jdbcTemplate.update(sql,id);
        return delete;
    }
}